from tkinter import *
from tkinter.ttk import *
from satellite_calculations import getData  # 导入计算模块

class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_label_top_text = self.__tk_label_top_text(self)
        self.tk_input_latitude_input = self.__tk_input_latitude_input(self)
        self.tk_input_longitude_input = self.__tk_input_longitude_input(self)
        self.tk_input_sat_longitude_input = self.__tk_input_sat_longitude_input(self)
        self.tk_label_top_text_1 = self.__tk_label_top_text_1(self)
        self.tk_label_top_text_2 = self.__tk_label_top_text_2(self)
        self.tk_label_top_text_3 = self.__tk_label_top_text_3(self)
        self.tk_button_cal_btn = self.__tk_button_cal_btn(self)
        self.tk_text_result_area = self.__tk_text_result_area(self)
        self.tk_label_buttom_text = self.__tk_label_buttom_text(self)

    def __win(self):
        self.title("桂林240台巡天助手 v1.0")
        width, height = 600, 350
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        self.geometry('%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2))
        self.resizable(width=False, height=False)

    def __tk_label_top_text(self, parent):
        label = Label(parent, text="输入 台站经纬度+卫星经度 点击计算即可（默认经纬度为桂林灵川）", anchor="center")
        label.place(x=0, y=0, width=600, height=36)
        return label

    def __tk_input_latitude_input(self, parent):
        ipt = Entry(parent)
        ipt.place(x=48, y=71, width=120, height=30)
        ipt.insert(0, "25.42")  # 默认值
        return ipt

    def __tk_input_longitude_input(self, parent):
        ipt = Entry(parent)
        ipt.place(x=240, y=72, width=120, height=30)
        ipt.insert(0, "110.33")  # 默认值
        return ipt

    def __tk_input_sat_longitude_input(self, parent):
        ipt = Entry(parent)
        ipt.place(x=419, y=72, width=120, height=30)
        ipt.insert(0, "115.5")  # 默认值
        return ipt

    def __tk_label_top_text_1(self, parent):
        label = Label(parent, text="当地纬度（北纬）", anchor="center")
        label.place(x=48, y=41, width=120, height=30)
        return label

    def __tk_label_top_text_2(self, parent):
        label = Label(parent, text="当地经度（东经）", anchor="center")
        label.place(x=240, y=41, width=120, height=30)
        return label

    def __tk_label_top_text_3(self, parent):
        label = Label(parent, text="卫星经度", anchor="center")
        label.place(x=419, y=41, width=120, height=30)
        return label

    def __tk_button_cal_btn(self, parent):
        btn = Button(parent, text="计算", takefocus=False, command=self.calculate_angles)
        btn.place(x=240, y=130, width=120, height=35)
        return btn

    def __tk_text_result_area(self, parent):
        text = Text(parent)
        text.place(x=200, y=196, width=200, height=100)
        return text

    def __tk_label_buttom_text(self, parent):
        label = Label(parent, text="本软件仅作学习卫星轨道角度计算所用", anchor="center")
        label.place(x=0, y=320, width=600, height=30)
        return label

    def calculate_angles(self):
        try:
            latitude = float(self.tk_input_latitude_input.get())
            longitude = float(self.tk_input_longitude_input.get())
            satellite_longitude = float(self.tk_input_sat_longitude_input.get())
            azimuth, elevation, lnb_skew = getData(latitude, longitude, satellite_longitude)
            result = (
                f"计算结果：\n"
                f"卫星锅方位角：{azimuth:.2f}°\n"
                f"卫星锅俯仰角：{elevation:.2f}°\n"
                f"高频头极化角：{lnb_skew:.2f}°"
            )
            self.tk_text_result_area.delete("1.0", END)
            self.tk_text_result_area.insert(END, result)
        except ValueError:
            self.tk_text_result_area.delete("1.0", END)
            self.tk_text_result_area.insert(END, "请输入有效的经纬度和卫星经度！")
        except Exception as e:
            self.tk_text_result_area.delete("1.0", END)
            self.tk_text_result_area.insert(END, f"发生错误：{str(e)}")

if __name__ == "__main__":
    win = WinGUI()
    win.mainloop()
